package com.evildog.workspace.gateway.config;

import com.evildog.workspace.common.util.IpUtil;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

/**
 * 限流配置
 */
@Configuration
public class RateLimitConfiguration {
    /**
     * 根据uri限流
     *
     * 公用的服务的限流
     */
    @Bean
    @Primary
    KeyResolver uriKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getURI().getPath());
    }

    /**
     * 根据ip限流
     */
    @Bean
    KeyResolver ipKeyResolver() {
        return exchange -> Mono.just(IpUtil.getRealIp(exchange.getRequest()));
    }
}
